package com.cloud.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pojo.Linkman;
import com.cloud.result.Result;
import com.cloud.service.LinkmanServiec;
import com.cloud.utils.GsonBuilderUtil;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping(value = "linkman")
public class LinkmanController {
	
	@Autowired
	private LinkmanServiec linkmanServiec;
	
	/**
	 * 添加联系人
	 * @param request
	 * @param linkman
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add" ,method = RequestMethod.POST)
	public String addLinkman(HttpServletRequest request,Linkman linkman){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		try {
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			 if(cookies!=null&&cookies.length>0){   
			        //遍历Cookie  
			        for(int i=0;i<cookies.length;i++){  
			            Cookie cookie=cookies[i];  
			            //此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
			            if("id".equals(cookie.getName())){  
			            	adminId_Str = cookie.getValue();  
			            }  
			        }  
			    }
			
			Integer adminId = Integer.valueOf(adminId_Str);
			if(linkman != null){
				if(StringUtils.isNotBlank(linkman.getName()) && StringUtils.isNotBlank(linkman.getPhone())){
					linkman.setAdminId(adminId);
					linkman.setCreateTime(new Date());
					Integer i = linkmanServiec.addLinkman(linkman);
					if(i > 0){
						result.setSuccess(i);
					}else{
						result.setFailure(1, "添加联系人失败");
					}
				}else{
					result.setFailure(1, "联系人和电话不能为空");
				}
			}else {
				result.setException("添加联系人参数传递异常");
			}
		} catch (Exception e) {
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	/**
	 * 批量上传联系人
	 * @param request
	 * @param linkmansJson
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addList" ,method = RequestMethod.POST)
	public String addLinkmanList(HttpServletRequest request,String linkmansJson){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		try {
			
			List<Linkman> linkmans = new ArrayList<>();
			linkmans = gson.fromJson(linkmansJson, new TypeToken<List<Linkman>>(){}.getType());
			
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			if(cookies!=null&&cookies.length>0){   
				//遍历Cookie  
				for(int i=0;i<cookies.length;i++){  
					Cookie cookie=cookies[i];  
					//此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
					if("id".equals(cookie.getName())){  
						adminId_Str = cookie.getValue();  
					}  
				}  
			}
			Integer adminId = Integer.valueOf(adminId_Str);
			if(adminId != null){
				if(linkmans != null && linkmans.size()>0){
					for (Linkman linkman : linkmans) {
						if(StringUtils.isNotBlank(linkman.getId())){
							
							if(StringUtils.isNotBlank(linkman.getName()) && StringUtils.isNotBlank(linkman.getPhone())){
								linkman.setAdminId(adminId);
								Integer i = linkmanServiec.addLinkmanList(linkman);
								if(i > 0){
									result.setSuccess(i);
								}else{
									result.setFailure(1, "上传联系人失败");
								}
							}else{
								result.setFailure(1, "上传的联系人和电话存在空值...");
								break;
							}
						}else {
							result.setFailure(1, "上传的联系人主键id存在空值...");
							break;
						}
					}
				}else {
					result.setFailure(1, "上传云端的联系人不能为空");
				}
			}else {
				result.setFailure(1, "获取用户id失败,可能cookie失效,请重新登陆...");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	/**
	 * 批量删除联系人
	 * @param request
	 * @param phonesJson 要删除的联系人的电话的json格式的list<String>集合
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteList" ,method = RequestMethod.POST)
	public String deleteLinkmanList(HttpServletRequest request,String idsJson){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		try {
			
			List<String> ids = new ArrayList<>();
			ids = gson.fromJson(idsJson, new TypeToken<List<String>>(){}.getType());
			
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			if(cookies!=null&&cookies.length>0){   
				//遍历Cookie  
				for(int i=0;i<cookies.length;i++){  
					Cookie cookie=cookies[i];  
					//此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
					if("id".equals(cookie.getName())){  
						adminId_Str = cookie.getValue();  
					}  
				}  
			}
			Integer adminId = Integer.valueOf(adminId_Str);
			if(adminId != null){
				if(ids != null && ids.size()>0){
					for (String id : ids) {
						if(StringUtils.isNotBlank(id)){
							Integer i = linkmanServiec.deleteLinkman(adminId, id);
							if(i > 0){
								result.setSuccess(i);
							}else{
								result.setFailure(1, "删除联系人失败");
							}
						}else{
							result.setFailure(1, "批量删除联系人的uuid有为空的参数");
						}
					}
				}else {
					result.setFailure(1, "请选择要删除的联系人");
				}
			}else {
				result.setFailure(1, "获取用户id失败,可能cookie失效,请重新登陆...");
			}
		} catch (Exception e) {
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete" ,method = RequestMethod.GET)
	public String deleteLinkman(HttpServletRequest request,String id){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		try {
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			if(cookies!=null&&cookies.length>0){   
				//遍历Cookie  
				for(int i=0;i<cookies.length;i++){  
					Cookie cookie=cookies[i];  
					//此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
					if("id".equals(cookie.getName())){  
						adminId_Str = cookie.getValue();  
					}  
				}  
			}
			Integer adminId = Integer.valueOf(adminId_Str);
			if(adminId != null){
				if(id != null && id != ""){
					Integer i = linkmanServiec.deleteLinkman(adminId,id);
					if(i > 0){
						result.setSuccess(i);
					}else {
						result.setFailure(1, "删除联系人失败");
					}
				}else {
					result.setFailure(1, "删除联系人的uuid不能为空");
				}
			}else {
				result.setFailure(1, "获取用户id失败,可能cookie失效,请重新登陆...");
			}
		} catch (Exception e) {
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/update" ,method = RequestMethod.POST)
	public String updateLinkman(HttpServletRequest request,Linkman linkman){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		try {
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			if(cookies!=null&&cookies.length>0){   
				//遍历Cookie  
				for(int i=0;i<cookies.length;i++){  
					Cookie cookie=cookies[i];  
					//此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
					if("id".equals(cookie.getName())){  
						adminId_Str = cookie.getValue();  
					}  
				}  
			}
			Integer adminId = Integer.valueOf(adminId_Str);
			if(adminId != null){
				if(linkman != null && StringUtils.isNotBlank(linkman.getId())){
					if(StringUtils.isNotBlank(linkman.getName()) && StringUtils.isNotBlank(linkman.getPhone())){
						linkman.setUpdateTime(new Date());
						Integer i = linkmanServiec.updateLinkman(adminId,linkman);
						if(i > 0){
							result.setSuccess(i);
						}else{
							result.setFailure(1, "编辑联系人信息失败");
						}
					}else{
						result.setFailure(1, "联系人和电话不能为空");
					}
				}else {
					result.setException("修改联系人参数传递异常,uuid不能为空");
				}
			}else {
				result.setFailure(1, "获取用户id失败,可能cookie失效,请重新登陆...");
			}
		} catch (Exception e) {
			result.setException(e);
		}
		
		return gson.toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/list" ,method = RequestMethod.GET)
	public String selectLinkmanList(HttpServletRequest request){
		Gson gson = GsonBuilderUtil.create();
		Result<List<Linkman>> result = new Result<>();
		try {
			String adminId = null;
			Cookie[] cookies = request.getCookies();
			 if(cookies!=null&&cookies.length>0){   
			        //遍历Cookie  
			        for(int i=0;i<cookies.length;i++){  
			            Cookie cookie=cookies[i];  
			            //此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
			            if("id".equals(cookie.getName())){  
			            	adminId = cookie.getValue();  
			            }  
			        }  
			    }  
			List<Linkman> linkmans = linkmanServiec.selectLinkmanList(Integer.valueOf(adminId));
			result.setSuccess(linkmans);
		} catch (Exception e) {
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	
}
